<template>
  <div class="profile-container">
    <!-- 头部 -->
    <div class="profile-header">
      <el-button 
        icon="ArrowLeft" 
        circle 
        @click="goBack"
        class="back-btn"
      />
      <h2 class="header-title">个人中心</h2>
      <el-button 
        icon="Setting" 
        circle 
        @click="openSettings"
      />
    </div>

    <!-- 用户信息卡片 -->
    <div class="user-info-card">
      <div class="user-avatar-section">
        <el-avatar 
          :size="80"
          :src="user?.avatar"
          :icon="user?.avatar ? undefined : 'User'"
        />
        <el-button 
          icon="Camera"
          circle
          size="small"
          class="avatar-edit-btn"
          @click="editAvatar"
        />
      </div>
      <div class="user-details">
        <h3 class="user-name">{{ user?.realName || user?.username }}</h3>
        <p class="user-id">ID: {{ user?.id }}</p>
        <p class="user-email" v-if="user?.email">{{ user.email }}</p>
      </div>
    </div>

    <!-- 徽章展示 -->
    <div class="badges-section">
      <div class="section-header">
        <h3>我的徽章</h3>
        <el-button type="text" @click="viewAllBadges">查看全部 →</el-button>
      </div>
      <div class="badges-grid">
        <div 
          v-for="badge in userBadges" 
          :key="badge.id"
          class="badge-item"
        >
          <div class="badge-icon" :class="badge.badge?.type">
            <el-icon :size="24">
              <component :is="badge.badge?.icon || 'Star'" />
            </el-icon>
          </div>
          <span class="badge-name">{{ badge.badge?.name }}</span>
        </div>
        <div v-if="userBadges.length === 0" class="no-badges">
          <el-icon size="32" color="#ccc"><Star /></el-icon>
          <p>暂无徽章</p>
        </div>
      </div>
    </div>

    <!-- 设置选项 -->
    <div class="settings-section">
      <el-card class="settings-card">
        <el-menu>
          <el-menu-item @click="openNotificationSettings">
            <div class="menu-item-content">
              <span>通知设置</span>
              <el-tag size="small" type="success">已开启</el-tag>
            </div>
          </el-menu-item>
          <el-menu-item @click="openAccountSecurity">
            <div class="menu-item-content">
              <span>账号与安全</span>
              <el-icon><ArrowRight /></el-icon>
            </div>
          </el-menu-item>
          <el-menu-item @click="openPrivacySettings">
            <div class="menu-item-content">
              <span>隐私设置</span>
              <el-icon><ArrowRight /></el-icon>
            </div>
          </el-menu-item>
          <el-menu-item @click="openAbout">
            <div class="menu-item-content">
              <span>关于我们</span>
              <el-icon><ArrowRight /></el-icon>
            </div>
          </el-menu-item>
        </el-menu>
      </el-card>
    </div>

    <!-- 退出登录 -->
    <div class="logout-section">
      <el-button 
        type="danger" 
        size="large"
        @click="handleLogout"
        :loading="logoutLoading"
      >
        退出登录
      </el-button>
    </div>

    <!-- 设置对话框 -->
    <el-dialog
      v-model="settingsVisible"
      title="设置"
      width="90%"
      :close-on-click-modal="false"
    >
      <div class="settings-content">
        <div class="setting-item">
          <h4>通知设置</h4>
          <el-switch 
            v-model="notificationSettings.pushEnabled"
            active-text="推送通知"
          />
          <el-switch 
            v-model="notificationSettings.emailEnabled"
            active-text="邮件通知"
          />
        </div>
        
        <div class="setting-item">
          <h4>隐私设置</h4>
          <el-switch 
            v-model="privacySettings.locationSharing"
            active-text="位置共享"
          />
          <el-switch 
            v-model="privacySettings.dataCollection"
            active-text="数据收集"
          />
        </div>
      </div>
      
      <template #footer>
        <el-button @click="settingsVisible = false">取消</el-button>
        <el-button type="primary" @click="saveSettings">保存</el-button>
      </template>
    </el-dialog>

    <!-- 关于我们对话框 -->
    <el-dialog
      v-model="aboutVisible"
      title="关于音乐节助手"
      width="90%"
      :close-on-click-modal="false"
    >
      <div class="about-content">
        <div class="app-info">
          <img src="/logo.svg" alt="音乐节助手" class="app-logo" />
          <h3>音乐节助手</h3>
          <p>Version 1.0.0</p>
        </div>
        
        <div class="about-description">
          <p>音乐节助手是一款专为音乐节参与者设计的智能应用，提供全方位的音乐节服务支持。</p>
          
          <h4>主要功能：</h4>
          <ul>
            <li>AI智能助手对话</li>
            <li>场地导航和设施查询</li>
            <li>艺人信息和演出管理</li>
            <li>粉丝互动和投票系统</li>
            <li>个人中心和数据管理</li>
          </ul>
          
          <h4>联系我们：</h4>
          <p>邮箱：support@musicfestival.com</p>
          <p>电话：400-123-4567</p>
        </div>
      </div>
      
      <template #footer>
        <el-button @click="aboutVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { getUserBadges } from '@/api/badge'
import type { UserBadge } from '@/types'

// 响应式数据
const logoutLoading = ref(false)
const settingsVisible = ref(false)
const aboutVisible = ref(false)
const userBadges = ref<UserBadge[]>([])

// 设置数据
const notificationSettings = ref({
  pushEnabled: true,
  emailEnabled: true
})

const privacySettings = ref({
  locationSharing: true,
  dataCollection: true
})

// 获取路由和用户store
const router = useRouter()
const userStore = useUserStore()

// 计算属性
const user = computed(() => userStore.user)

// 返回上一页
const goBack = () => {
  router.back()
}

// 打开设置
const openSettings = () => {
  settingsVisible.value = true
}

// 编辑头像
const editAvatar = () => {
  ElMessage.info('头像编辑功能开发中...')
}

// 查看所有徽章
const viewAllBadges = () => {
  ElMessage.info('徽章详情页面开发中...')
}

// 打开通知设置
const openNotificationSettings = () => {
  settingsVisible.value = true
}

// 打开账号安全
const openAccountSecurity = () => {
  ElMessage.info('账号安全页面开发中...')
}

// 打开隐私设置
const openPrivacySettings = () => {
  settingsVisible.value = true
}

// 打开关于我们
const openAbout = () => {
  aboutVisible.value = true
}

// 保存设置
const saveSettings = () => {
  ElMessage.success('设置已保存')
  settingsVisible.value = false
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm(
      '确定要退出登录吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    logoutLoading.value = true
    await userStore.logout()
    
    ElMessage.success('已退出登录')
    router.push('/login')
  } catch (error) {
    if (error !== 'cancel') {
      console.error('退出登录失败:', error)
      ElMessage.error('退出登录失败')
    }
  } finally {
    logoutLoading.value = false
  }
}

// 获取用户徽章
const fetchUserBadges = async () => {
  try {
    const response = await getUserBadges()
    userBadges.value = response.data
  } catch (error) {
    console.error('获取用户徽章失败:', error)
  }
}

// 页面挂载时执行
onMounted(() => {
  fetchUserBadges()
})
</script>

<style lang="scss" scoped>
.profile-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding-bottom: 20px;
}

.profile-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.back-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.user-info-card {
  background: white;
  margin: 20px;
  padding: 30px;
  border-radius: 15px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  text-align: center;
}

.user-avatar-section {
  position: relative;
  display: inline-block;
  margin-bottom: 20px;
}

.avatar-edit-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  background: #667eea;
  border: 2px solid white;
}

.user-details {
  .user-name {
    font-size: 24px;
    font-weight: 600;
    color: #333;
    margin: 0 0 5px 0;
  }
  
  .user-id {
    font-size: 14px;
    color: #666;
    margin: 0 0 5px 0;
  }
  
  .user-email {
    font-size: 14px;
    color: #999;
    margin: 0;
  }
}

.badges-section {
  margin: 20px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  
  h3 {
    font-size: 18px;
    font-weight: 600;
    color: #333;
    margin: 0;
  }
}

.badges-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 15px;
}

.badge-item {
  text-align: center;
  padding: 15px 10px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  
  .badge-icon {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 10px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
  }
  
  .badge-name {
    font-size: 12px;
    color: #666;
  }
}

.no-badges {
  grid-column: 1 / -1;
  text-align: center;
  padding: 30px;
  color: #999;
  
  p {
    margin: 10px 0 0 0;
    font-size: 14px;
  }
}

.settings-section {
  margin: 20px;
}

.settings-card {
  border-radius: 15px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.menu-item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.logout-section {
  margin: 20px;
  text-align: center;
}

.settings-content {
  .setting-item {
    margin-bottom: 30px;
    
    h4 {
      font-size: 16px;
      font-weight: 600;
      color: #333;
      margin-bottom: 15px;
    }
    
    .el-switch {
      display: block;
      margin-bottom: 10px;
    }
  }
}

.about-content {
  .app-info {
    text-align: center;
    margin-bottom: 30px;
    
    .app-logo {
      width: 60px;
      height: 60px;
      margin-bottom: 15px;
    }
    
    h3 {
      font-size: 20px;
      font-weight: 600;
      color: #333;
      margin: 0 0 5px 0;
    }
    
    p {
      color: #666;
      margin: 0;
    }
  }
  
  .about-description {
    h4 {
      font-size: 16px;
      font-weight: 600;
      color: #333;
      margin: 20px 0 10px 0;
    }
    
    p {
      color: #666;
      line-height: 1.6;
      margin-bottom: 10px;
    }
    
    ul {
      color: #666;
      line-height: 1.6;
      margin-bottom: 15px;
      padding-left: 20px;
      
      li {
        margin-bottom: 5px;
      }
    }
  }
}

// 移动端适配
@media screen and (max-width: 768px) {
  .profile-header {
    padding: 12px 15px;
  }
  
  .header-title {
    font-size: 16px;
  }
  
  .user-info-card {
    margin: 15px;
    padding: 20px;
  }
  
  .user-name {
    font-size: 20px;
  }
  
  .badges-section,
  .settings-section,
  .logout-section {
    margin: 15px;
  }
  
  .badges-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
  }
  
  .badge-item {
    padding: 12px 8px;
    
    .badge-icon {
      width: 40px;
      height: 40px;
    }
    
    .badge-name {
      font-size: 11px;
    }
  }
}
</style> 